function atom(params) {
	const dask = require("dask");
	const numjs = require('numjs');

	console.debug || (console.debug = console.log);

	function dataInput(condition) {
		return function (param_type, field_name) {
			console.log("get param start-------------");
			if (!condition[param_type]) {
				console.error(`please check condtion`);
				return;
			}
			const param = condition[param_type];
			if (!param[field_name]) {
				console.error(`please check param ,has no param ${field_name}`);
				return;
			}
			const res = param[field_name];
			console.debug("get param end-------------", res);
			return res;
		}
	}

	function readFromDatabase(condition) {
		return async function (query_mode, spaceid, systemid, tablename, fields, criteria, page_no, page_size, sort, sql) {
			const query = [{
				"query_mode": query_mode,
				"spaceid": spaceid,
				"systemid": systemid,
				"tablename": tablename,
				"criteria": criteria,
				"fields": fields,
				"page_no": page_no,
				"page_size": page_size,
				"sort": sort,
				"sql": sql
			}];
			// console.info("query---------", query);
			const result = await send_msg("dbacc.find.intranet",query);
			// console.log("result===========", result);
			if (result.length == 0) {
				console.error(`get none data`);
				return numjs.array();
			};
			const res = _docode(result, tablename, fields);
			console.log("---------", res);
			return res;
		}
	}

	function _docode(views, tablename, fields) {
		for (let i = 0; i < views.length; i++) {
			const view = views[i];
			if (view["tablename"] == tablename) {
				const data_array = view["dataArray"];
				console.log("data_array==", data_array);
				const num_array = numjs.array(data_array);
				console.log(num_array);
				// return num_array;
			} else {
				const columns = fields.keys();
				return numjs.array(columns);
			}
		};
		const columns = fields.keys();
		return numjs.array(columns);
	}

	function generateVariable(condition) {
		return function (data, field_name, var_name, data_type, get_type) {
			let c_data = data.clone();
			switch (get_type) {
				case "first":
					c_data = c_data.slice([1]);
					let r_data = c_data.get(0);
					return r_data[field_name];
				// if (var_name || data_type) {
				//     let r_data = c_data.get(0);
				//     if (data_type) {
				//         r_data[field_name] = "aa";
				//     }
				//     if (var_name) {
				//         r_data[var_name] = r_data[field_name];
				//     }
				// }
				// break;
			};
			return c_data;
		}
	}

	function datarender(condition) {
		const dotjs = require("dot");
		return function (data, tpl, p_name) {
			const html = dotjs.template(tpl)(data);
			let res = {};
			res[p_name] = html;
			return res;
		}
	}

	function link(condition) {
		return function () { }
	}

	const feidao = {
		dataInput: dataInput(params),
		readFromDatabase: readFromDatabase(params),
		generateVariable: generateVariable(params),
		datarender: datarender(params),
		link: link(params)
	};
	const dsk = {
		"dataInput_1": {
			"dataInput": ["param", "advertising_no"]
		},
		"dataInput_2": {
			"dataInput": ["param", "page_no"]
		},
		"dataInput_3": {
			"dataInput": ["param", "component_name"]
		},
		"dataInput_4": {
			"dataInput": ["param", "presentation_name"]
		},
		"fields1": { "_id": 1, "ad_type": 1, "advertising_no": 1, "content": 1, "link_url": 1, "order": 1, "status": 1 },
		"criteria1": { "advertising_no": "GG00004" },
		"data": {
			"readFromDatabase": [1, "medicine_store2017", "1091", "detail_advertising", "fields1", "criteria1", 1, 20, {}, "sql"]
		},
		"fields2": { "component_name": 1, "file_cont": 1, "productid": 1, "page_no": 1, "presentation_name": 1, "_id": 1 },
		'p1': {
			fun2: ['dataInput_1', 'page_no']
		},
		'p2': {
			fun2: ['dataInput_2', 'component_name']
		},
		'p3': {
			fun2: ['dataInput_3', 'presentation_name']
		},
		"criteria2": {
			fun: ['p1', 'p2', 'p3']
		},
		"readFromDatabase_2": {
			"readFromDatabase": [1, "medicine_store2017", "11", "base_tpl", "fields2", "criteria2", 0, 20, {}, ""]
		},
		"tpl": {
			"generateVariable": ["readFromDatabase_2", "file_cont", "", "string", "first"]
		},
		"datarender1": {
			"datarender": ["data", "tpl", "P_01"]
		},
		"r": {
			"link": ["datarender1"]
		}
	}
	return dask.get(dsk, "r", feidao);
}

exports.spaceid = 'feidao_public';
const params = {
	"param": {
		"page_no": "YM2017030300000017",
		"component_name": "ZJ2017040600000994",
		"presentation_name": "P_01",
		"advertising_no": "GG00004"
	}
}

const code = atom.toString();
exports.code = code;
if (require.main === module) {
	const iv = require('../dist/invoke');
	iv.default({ code, params }).then((rc) => {
		console.log('=========================================');
		console.log(JSON.stringify(rc));
	}, (err) => {
		console.error(err);
	});
}
